package org.gy.T2;

import org.gy.T1.c.Student;

import java.util.Arrays;
import java.util.Comparator;
import java.util.function.IntToDoubleFunction;

public class LambdaTest2 {
    public static void main(String[] args) {
// ⽬标：使⽤Lambda简化函数式接⼝。
        double[] prices = {99.8, 128, 100};
        //1.对数组中的每⼀个元素*0.8: 匿名内部类写法
        Arrays.setAll(prices, new IntToDoubleFunction() {
            @Override
            public double applyAsDouble(int value) {
// value = 0 1 2
                return prices[value] * 0.8;
            }
        });
        //2.需求：对数组中的每⼀个元素*0.8,使⽤Lambda表达式标准写法
        Arrays.setAll(prices, (int value) -> {
            return prices[value] * 0.8;
        });
        //3.使⽤Lambda表达式简化格式1——省略参数类型
        Arrays.setAll(prices, (value) -> {
            return prices[value] * 0.8;
        });
        //4.使⽤Lambda表达式简化格式2——省略()
        Arrays.setAll(prices, value -> {
            return prices[value] * 0.8;
        });
//5.使⽤Lambda表达式简化格式3——省略{}
        Arrays.setAll(prices, value -> prices[value] * 0.8 );
        System.out.println(Arrays.toString(prices));
        System.out.println("------------------------------------");
        Student[] students = new Student[4];
        students[0] = new Student("蜘蛛精", 169.5, 23);
        students[1] = new Student("紫霞", 163.8, 26);
        students[2] = new Student("紫霞", 163.8, 26);
        students[3] = new Student("⾄尊宝", 167.5, 24);

//1.使⽤匿名内部类
        Arrays.sort(students, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return Double.compare(o1.getHeight(), o2.getHeight()); // 升序
            }
        });
        //2.使⽤Lambda表达式表达式——标准格式
        Arrays.sort(students, (Student o1, Student o2) -> {
            return Double.compare(o1.getHeight(), o2.getHeight()); // 升序
        });
        //3.使⽤Lambda表达式表达式——省略参数类型
        Arrays.sort(students, ( o1, o2) -> {
            return Double.compare(o1.getHeight(), o2.getHeight()); // 升序
        });
        //4.使⽤Lambda表达式表达式——省略{}
        Arrays.sort(students, ( o1, o2) -> Double.compare(o1.getHeight(), o2.getHeight()));
        System.out.println(Arrays.toString(students));
    }
}
